package com.taotao.manage.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.taotao.common.utils.EasyUIResult;
import com.taotao.manage.pojo.ItemParam;
import com.taotao.manage.service.ItemParamService;

@Controller
@RequestMapping("item/param")
public class ItemParamController {

	@Autowired
	private ItemParamService itemParamService;
	
	private static final Logger LOGGER =LoggerFactory.getLogger(ItemParamController.class);
	/**
	 * 根据类目的ID查询是否存在参数模板
	 * @param itemCatId
	 * @return
	 */
	@RequestMapping(value="{itemCatId}",method=RequestMethod.GET)
	public ResponseEntity<ItemParam> queryItemParamByItemCatId(@PathVariable("itemCatId") Long itemCatId) {
		try {
			ItemParam record  = new ItemParam();
			record.setItemCatId(itemCatId);
			ItemParam itemParam  = itemParamService.queryOne(record);
			if (null==itemParam) {
				//未找到，返回404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}else {
				return ResponseEntity.status(HttpStatus.OK).body(itemParam);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
		}
		
	}
	
	/**
	 * 根据商品类目ID新增参数模板
	 * @param itemCatId
	 * @return
	 */
	@RequestMapping(value="{itemCatId}",method=RequestMethod.POST)
	public ResponseEntity<Void> InsertItemParamByItemCatId(@PathVariable("itemCatId") Long itemCatId,
													@RequestParam("paramData") String paramData) {
		try {
			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug("开始新增商品参数模板！ itemCatId={},paramData={}",itemCatId,paramData);
			}
			ItemParam record  = new ItemParam();
			record.setItemCatId(itemCatId);
			record.setParamData(paramData);
			Integer count = itemParamService.save(record);
			if (count==1) {
				if (LOGGER.isDebugEnabled()) {
					LOGGER.debug("新增商品参数模板成功！ itemCatId={},paramData={}",itemCatId,paramData);
				}
				//增加成功，返回201
				return ResponseEntity.status(HttpStatus.CREATED).build();
			}else {
				if (LOGGER.isDebugEnabled()) {
					LOGGER.debug("新增商品参数模板参数不对！ itemCatId={},paramData={}",itemCatId,paramData);
				}
				//增加参数不对，返回400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug("新增商品参数模板参数失败！ itemCatId={},paramData={}",itemCatId,paramData,e);
			}
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
		
	}
	
	/**
	 * 查询参数模板列表
	 * @param itemCatId
	 * @return
	 */
	@RequestMapping(value="list",method=RequestMethod.GET)
	public ResponseEntity<EasyUIResult> queryItemParamList(@RequestParam(value="page",defaultValue="1") Integer page,
										@RequestParam(value="rows",defaultValue="30") Integer rows) {
		try {
			PageInfo<ItemParam> pageInfo  = itemParamService.queryItemParamPageListByUpdate(page, rows);
			if (pageInfo==null) {
				//未找到，返回404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			EasyUIResult easyUIResult = new EasyUIResult(pageInfo.getTotal(),pageInfo.getList());
			return ResponseEntity.status(HttpStatus.OK).body(easyUIResult);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
		}
		
	}
}